 //
//  WYDynamicCell.m
//  Boss
//
//  Created by admin on 2018/7/5.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYDynamicCell.h"
#import "WYHonnorCollectionViewCell.h"
#import "WYUserMessage.h"
#import <KSPhotoBrowser/KSPhotoBrowser.h>
@interface WYDynamicCell()<UICollectionViewDelegate,UICollectionViewDataSource,KSPhotoBrowserDelegate,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) IBOutlet UIImageView *userImageView;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *timeLabel;

@property (strong, nonatomic) IBOutlet UILabel *contentLabel;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) IBOutlet UILabel *seeCount;
//@property (strong, nonatomic) IBOutlet UIButton *messageCount;
//@property (strong, nonatomic) IBOutlet UIButton *shareCount;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHeight;
@property (nonatomic,strong)NSArray *dataArray;
@property (strong, nonatomic) IBOutlet UIButton *deleteBtn;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *deleteBtnWidth;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *deleteBtnHeight;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineTopContant;

@property (strong, nonatomic) IBOutlet UILabel *addressLabel;

@property (strong, nonatomic) IBOutlet UILabel *shareLabel;

@property (strong, nonatomic) IBOutlet UILabel *messageLabel;



@end
@implementation WYDynamicCell
-(void)setDyc:(WYMYDynamic *)dyc
{
    _dyc = dyc;
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"ic_defult_avatar"]];
    if (dyc.pageView.integerValue>10000) {
        self.seeCount.text = [NSString stringWithFormat:@"%.2f万阅读量",dyc.pageView.floatValue/10000];
    }else{
        self.seeCount.text = [NSString stringWithFormat:@"%@ 阅读量",dyc.pageView];
    }
    self.addressLabel.text = dyc.address;
    
  //  self.contentLabel.text = dyc.content;
    
    self.contentLabel.attributedText = [self setTextString:dyc.content];
    
    self.timeLabel.text =[NSString compareTimeWithBossTime:dyc.createTime];
    //[self.messageCount setTitle:dyc.commentNum forState:UIControlStateNormal];
    self.messageLabel.text = dyc.commentNum;
  //  [self.shareCount setTitle:dyc.transpondNum forState:UIControlStateNormal];
    self.shareLabel.text = dyc.transpondNum;
     WYUserMessage *userMessage = [WYStoreModule currentUserMessage];
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:userMessage.url] placeholderImage:[UIImage imageNamed:@"ic_defult_avatar"]];
    self.nameLabel.text = userMessage.userName;
    
    if (dyc.state.integerValue) {
        self.deleteBtn.hidden = NO;
    }else{
        self.deleteBtn.hidden = YES;
    }
    self.dataArray = [dyc.url componentsSeparatedByString:@","];
    if (self.dataArray.count) {
        self.lineTopContant.constant = 16;
    }else{
        self.lineTopContant.constant = 0;
    }
    [self reloadCell];
    self.collectionView.bounces = NO;
    [self.deleteBtn setImage:[UIImage imageNamed:@"dymic_delete"] forState:(UIControlStateNormal)];
}

-(NSAttributedString *)setTextString:(NSString *)text

{
    
    NSMutableAttributedString *mAbStr = [[NSMutableAttributedString alloc] initWithString:text];
    
    NSMutableParagraphStyle *npgStyle = [[NSMutableParagraphStyle alloc] init];
    
    npgStyle.alignment = NSTextAlignmentJustified;
    
    npgStyle.paragraphSpacing = 11.0;
    
    npgStyle.paragraphSpacingBefore = 10.0;
    
    npgStyle.firstLineHeadIndent = 0.0;
    
    npgStyle.lineSpacing = 4;
    
    npgStyle.headIndent = 0.0;
    
    NSDictionary *dic = @{
                          
                          NSForegroundColorAttributeName:[UIColor blackColor],
                          
                          NSFontAttributeName           :[UIFont systemFontOfSize:15.0],
                          
                          NSParagraphStyleAttributeName :npgStyle,
                          
                          NSUnderlineStyleAttributeName :[NSNumber numberWithInteger:NSUnderlineStyleNone]
                          
                          };
    
    [mAbStr setAttributes:dic range:NSMakeRange(0, mAbStr.length)];
    
    NSAttributedString *attrString = [mAbStr copy];
    
    return attrString;
    
}





-(void)setHistory:(WYMYDynamic *)history
{
    _history = history;
    
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"ic_defult_avatar"]];
//    self.seeCount.text = [NSString stringWithFormat:@"%@ 次观看",history.pageView];
    if (history.pageView.integerValue>10000) {
        self.seeCount.text = [NSString stringWithFormat:@"%.2f万阅读量",history.pageView.floatValue/10000];
    }else{
        self.seeCount.text = [NSString stringWithFormat:@"%@ 阅读量",history.pageView];
    }
    self.contentLabel.text = history.content;
    self.timeLabel.text = [NSString compareTimeWithBossTime:history.createTime];
    self.messageLabel.text = history.commentNum;
    //  [self.shareCount setTitle:dyc.transpondNum forState:UIControlStateNormal];
    self.shareLabel.text = history.transpondNum;
    
    WYUserMessage *userMessage = [WYStoreModule currentUserMessage];
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:userMessage.url] placeholderImage:[UIImage imageNamed:@"ic_defult_avatar"]];
    self.nameLabel.text = userMessage.userName;
    self.deleteBtn.hidden = NO;
//    if (history.state.integerValue) {
//
//    }else{
//        self.deleteBtn.hidden = YES;
//    }
    self.dataArray = [history.url componentsSeparatedByString:@","];
    [self reloadCell];
    self.collectionView.bounces = NO;
    
    [self.deleteBtn setTitle:@" 导入 " forState:UIControlStateNormal];
    [self.deleteBtn setTitleColor:kColorMainColor forState:(UIControlStateNormal)];
    [self.deleteBtn setBorderWithWidth:kLineHeight andColor:kColorMainColor];
    
    self.deleteBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    self.deleteBtnWidth.constant = 60;
    self.deleteBtnHeight.constant = 28;
    
    
    
    
    
}
- (void)reloadCell{
    [self.collectionView reloadData];
    self.collectionView.userInteractionEnabled = NO;
    self.collectionViewHeight.constant = 0;
    if (self.dataArray.count) {
        if (self.dataArray.count<=3) {
            //return 290;
            self.collectionViewHeight.constant = (kScreenWidth-30-32)/3.0;
        }
        if (self.dataArray.count>3&&self.dataArray.count<=6) {
            self.collectionViewHeight.constant = (kScreenWidth-30-32)/3.0*2+10;
            //return 390;
        }
        if (self.dataArray.count>6&&self.dataArray.count<=9) {
            self.collectionViewHeight.constant = (kScreenWidth-30-32)/3.0*3+20;
          //  return 490;
        }


    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSMutableArray *items = @[].mutableCopy;
//    for (int i = 0; i < self.dataArray.count; i++) {
//        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
//        WYHonnorCollectionViewCell *cell =( WYHonnorCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//        UIImageView *imgV = cell.honnorImageView;
//        UIImage *image =cell.honnorImageView.image;
//        KSPhotoItem *item = [KSPhotoItem itemWithSourceView:imgV image:image];
//        [items addObject:item];
//    }
//    KSPhotoBrowser *browser = [KSPhotoBrowser browserWithPhotoItems:items selectedIndex:indexPath.row];
//    browser.delegate = self;
//    browser.dismissalStyle = KSPhotoBrowserInteractiveDismissalStyleRotation;
//    browser.backgroundStyle = KSPhotoBrowserBackgroundStyleBlurPhoto;
//    browser.loadingStyle = KSPhotoBrowserImageLoadingStyleDeterminate;
//    browser.pageindicatorStyle = KSPhotoBrowserPageIndicatorStyleDot;
//    browser.bounces = NO;
////    [browser showFromViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
//    [browser showFromViewController:self.selfVc];
//    
    
}
- (void)ks_photoBrowser:(KSPhotoBrowser *)browser didSelectItem:(KSPhotoItem *)item atIndex:(NSUInteger)index
{
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUpUI];
    
}
-(void)setUpUI
{
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake((kScreenWidth-30-32)/3.0, (kScreenWidth-30-32)/3.0);
    //横向最小距离
//    layout.minimumInteritemSpacing=0.0f;
//    layout.minimumLineSpacing=10.0f;
    //cell距离上  左  下 右 的距离
//    layout.sectionInset=UIEdgeInsetsMake(10, 0, 10, 10);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [self.collectionView setCollectionViewLayout:layout];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"WYHonnorCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"WYHonnorCollectionViewCell"];
    
    [self.userImageView setCornerRadius:20.f];
    
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WYHonnorCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"WYHonnorCollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYHonnorCollectionViewCell" owner:nil options:nil].firstObject;
    }
    [cell.honnorImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",self.dataArray[indexPath.row]]] placeholderImage:[UIImage imageNamed:@"logo_place"]];
    
    return cell;
}

- (IBAction)deleteAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(deleteActionWithDymic:)]) {
        [self.delegate deleteActionWithDymic:self.dyc?self.dyc:self.history];
    }
    
    
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((self.collectionView.width-30)/3.0, (self.collectionView.width-30)/3.0);
}
- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10.0f;
}
- (CGFloat) collectionView:(UICollectionView *)collectionView
                    layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10.0f;
}
@end
